package com.markchu.modules.system.service.impl;

import com.markchu.common.data.PageSort;
import com.markchu.common.enums.StatusEnum;
import com.markchu.modules.system.domain.ActivityJoinUser;
import com.markchu.modules.system.domain.Upload;
import com.markchu.modules.system.enums.FileTypeEnum;
import com.markchu.modules.system.repository.ActivityJoinUserRepository;
import com.markchu.modules.system.service.ActivityJoinUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author mark_chu
 * @date 2023/11/22
 */
@Service
public class ActivityJoinUserServiceImpl implements ActivityJoinUserService {

    @Autowired
    private ActivityJoinUserRepository activityJoinUserRepository;

    /**
     * 根据ID查询数据
     * @param id 主键ID
     */
    @Override
    public ActivityJoinUser getById(Long id) {
        return activityJoinUserRepository.findById(id).orElse(null);
    }
    @Override
    public boolean getObjectBy(Long userId,Long acId){
        int counts  = activityJoinUserRepository.getObjectBy(userId,acId);
        if(counts>=1){
            return false;
        }
        return true;
    }

    @Override
    public List<ActivityJoinUser> getListBy(Long activeId,String orderBy) {
        ExampleMatcher matcher = ExampleMatcher.matching();
        ActivityJoinUser activityJoinUser = new ActivityJoinUser();
        activityJoinUser.setAc_id(activeId);
        activityJoinUser.setStatus(StatusEnum.OK.getCode());

        Example<ActivityJoinUser> example = Example.of(activityJoinUser, matcher);
        Sort sort = Sort.unsorted();
        if(StringUtils.isNotEmpty(orderBy)){
            sort = Sort.by(Sort.Direction.DESC, "clicks");
        }
        return activityJoinUserRepository.findAll(example,sort);
    }

    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<ActivityJoinUser> getPageList(Example<ActivityJoinUser> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        return activityJoinUserRepository.findAll(example, page);
    }

    /**
     * 保存数据
     * @param activityJoinUser 实体对象
     */
    @Override
    public ActivityJoinUser save(ActivityJoinUser activityJoinUser) {
        return activityJoinUserRepository.saveAndFlush(activityJoinUser);
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
        return activityJoinUserRepository.updateStatus(statusEnum.getCode(), idList) > 0;
    }
}